<?php
class xp {
	public function getXpath($url, $debug=0) {
		
		$doc = new DOMDocument();
		
		if($debug) {
			if(! is_file('data.php')) {
				die('Data file not defined!');	
			}				
			include('data.php');
			$file_contents = ob_get_clean();		
			$doc->loadHTML($file_contents);
		} else {
			$file_contents = file_get_contents($url);
			$doc->loadHTML($file_contents);
		}
	
		$xpath = new DOMXpath($doc);
		
		return $xpath; 	
	}
	
	public function getData($action, $parms) {
		$data = array();
		
		$previous_value = libxml_use_internal_errors(TRUE);
		
		switch($action) {
			case 'company-info':
				$url = $parms;				
				$price_url = "http://money.rediff.com/money1/current_stat.php?companyCode=".basename($url);				
				$xpath = $this->getXpath($url);				
				$xpath2 = $this->getXpath($price_url);
				$company_name = $xpath->query("//div[@id='leftcontainer']/h1")->item(0)->nodeValue;
				$stock_price = $xpath2->query("//span[@id='ltpid']")->item(0)->nodeValue;
				$image_url = $xpath->query("//div[@class='m_sectionleft']/div/div/img")->item(0)->getAttribute('src');
				$hotandcold = preg_replace('/[^-0-9]/','', basename($image_url)); 
				$bsc_code = $xpath->query("//div[@id='leftcontainer']/div/span")->item(1)->nodeValue;
				$bsc_code = preg_replace('/[^0-9]/','', $bsc_code);
				$data = array(
					'name' => $company_name,
					'price' => $stock_price,
					'rank' => $hotandcold,
					'bsc_code' => $bsc_code, 
				);
				
				return $data;
			break;
			case 'company-list':
				$url = $parms;
				$xpath = $this->getXpath($url);
				$elements = $xpath->query("//table[@class='dataTable']/tbody/tr");
		
			//	echo $elements->length;
				
				if (!is_null($elements)) {
				  foreach ($elements as $element) {
				  	
				  	$elements = $xpath->query("td/a", $element);
				  	$company_url = $elements->item(0)->getAttribute('href');
				  	$company_name = $elements->item(0)->nodeValue;
				  	$company_code = basename($company_url);
				  	$data[$company_code] = array(
				  		'name' => $company_name,
				  		'url' => $company_url,
				  		'code' => $company_code, 
				  	);
				  }
				}	
				
				return $data;
			break;			
			case 'stock-move-day':
				$company_code = $parms;
				$url = "http://www.bseindia.com/stockinfo/stockprc2.aspx?scripcode=$parms&flag=sp&Submit=G";
				$xpath = $this->getXpath($url);
				$elements = $xpath->query("//span[@id='spnStkData']/table/tr");
								
//				echo $elements->length;
		
				if (!is_null($elements)) {
				  
				  foreach ($elements as $element) {
				  	
					$row = $xpath->query("td", $element);					  					  	
					$key_code = $row->item(0)->nodeValue;
					
					if(!empty($key_code)) {				
					  	$data[$key_code]['day'] = $key_code;
					  	$data[$key_code]['code'] = $company_code;  	
					  	$data[$key_code]['open_price'] = $row->item(1)->nodeValue;
					  	$data[$key_code]['height_price'] = $row->item(2)->nodeValue;
					  	$data[$key_code]['low_price'] = $row->item(3)->nodeValue;
					  	$data[$key_code]['close_price'] = $row->item(4)->nodeValue;
					  	$data[$key_code]['wap'] = $row->item(5)->nodeValue;
					  	$data[$key_code]['nps'] = $row->item(6)->nodeValue;
					  	$data[$key_code]['not'] = $row->item(7)->nodeValue;
					  	$data[$key_code]['to'] = $row->item(8)->nodeValue;	
				  	}				  					  	
				  }
				}
				
				return $data;
				
			break;
			case 'stock-move-month':
				$company_code = $parms;
				$from_date = date("m/d/Y", strtotime("-1 year"));
				$to_date = date("m/d/Y");				
				$url = "http://www.bseindia.com/stockinfo/stockprc2.aspx?scripcode=$parms&fromDate=$from_date&toDate=$to_date&strDMY=M";
				$xpath = $this->getXpath($url);
				$elements = $xpath->query("//span[@id='spnStkData']/table/tr");
								
//				echo $elements->length;
		
				if (!is_null($elements)) {

				  foreach ($elements as $element) {

				  	$row = $xpath->query("td", $element);					  					  	
				  	$key_code = $row->item(0)->nodeValue;
				  	
				  	if(!empty($key_code)) {				
					  	$data[$key_code]['month'] = $key_code;
					  	$data[$key_code]['code'] = $company_code;  	
					  	$data[$key_code]['open_price'] = $row->item(1)->nodeValue;
					  	$data[$key_code]['height_price'] = $row->item(2)->nodeValue;
					  	$data[$key_code]['low_price'] = $row->item(3)->nodeValue;
					  	$data[$key_code]['close_price'] = $row->item(4)->nodeValue;
					  	$data[$key_code]['wap'] = $row->item(5)->nodeValue;
					  	$data[$key_code]['nps'] = $row->item(6)->nodeValue;
					  	$data[$key_code]['not'] = $row->item(7)->nodeValue;
					  	$data[$key_code]['to'] = $row->item(8)->nodeValue;
				  	}
				  }
				}
				
				return $data;
				
			break;
			case '-':
			break;
			default;
				return array();
			break;
		}
		
		libxml_clear_errors();
  		libxml_use_internal_errors($previous_value);
	}
}
?>